//
//  MarketListView.m
//  AppleMarket
//
//  Created by yangy on 19/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "MarketListView.h"

// view
#import "MarketListCell.h"
// viewModel
#import "MarkListViewModel.h"

@interface MarketListView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *sectionHeadView;
@property (nonatomic, strong) MarkListViewModel *viewModel;
@end

@implementation MarketListView


- (instancetype)initWithFrame:(CGRect)frame viewModel:(id) viewModel;
{
    self = [super initWithFrame:frame];
    if(self){
        _viewModel = viewModel;
        [self addSubview:self.tableView];
        
        @weakify(self);
        [_viewModel.refershMarketEndSubject subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            [self.tableView reloadData];
            if(self.heightChangeBlock){
                self.heightChangeBlock();
            }
        }];
    }
    return self;
}

- (CGFloat) heightForTableView
{
    [self.tableView layoutIfNeeded];
    self.tableView.height = self.tableView.contentSize.height;
    self.height = self.tableView.height;
    return self.height;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MarketListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MarketListCell class]) forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell updateWithModel:self.viewModel.dataList[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 47;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    return self.sectionHeadView;
}

- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_tableView registerClass:[MarketListCell class] forCellReuseIdentifier:NSStringFromClass([MarketListCell class])];
        [_tableView setScrollEnabled:NO];
    }
    return _tableView;
}

- (UIView *)sectionHeadView
{
    if(!_sectionHeadView){
        _sectionHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, k_ScreenWidth, 47)];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_sectionHeadView.frame) - 1, k_ScreenWidth, 1)];
        lineView.backgroundColor = kColorWithHex(0xEFEFEF);
        UILabel *label = [UILabel cm_labelWithText:@"APPLE行情" font:kFontBold(16) textColor:kColorWithHex(0x0E304C)];
        label.frame = _sectionHeadView.bounds;
        label.x = 15;
        [_sectionHeadView addSubview:label];
        [_sectionHeadView addSubview:lineView];
    }
    return _sectionHeadView;
}

@end
